package com.hnevc.trafficdemotest1.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.google.gson.Gson;
import com.hnevc.trafficdemotest1.R;
import com.hnevc.trafficdemotest1.entity.AllCarPeccancy;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2018/1/3/003.
 */

public class DataAnalysisViewPageFragment2 extends Fragment {
    int mCarNoPeccancyNum, mCarAllPeccancyNum;
    View view;
    PieChart mPieChart;
    int leng;
    int mCarPeccancyNum;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.data_analysis_viewpage_fragment2, container, false);

        mPieChart = (PieChart) view.findViewById(R.id.data_analysis_viewPage2_pie);
        mPieChart.setUsePercentValues(true);
        mPieChart.setDescription("");
        mPieChart.setExtraOffsets(5, 10, 5, 5);

        mPieChart.setDragDecelerationFrictionCoef(0.95f);


        mPieChart.setDrawHoleEnabled(true);
        mPieChart.setHoleColorTransparent(true);

        mPieChart.setTransparentCircleColor(Color.WHITE);
        mPieChart.setTransparentCircleAlpha(110);

        mPieChart.setHoleRadius(0f);
        mPieChart.setTransparentCircleRadius(0f);

        mPieChart.setDrawCenterText(true);

        mPieChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mPieChart.setRotationEnabled(true);

        Legend l = mPieChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(3f);

        mPieChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
        getAllCarPeccancyNum();
        return view;

    }

    private void getAllCarPeccancyNum() {
        HashMap<String, String> map = new HashMap<>();
        map.put("userName", "user1");
        JSONObject object = new JSONObject(map);
        String url = "http://192.168.1.142:8080/RestaurantServer/action/getAllCarPeccancy.do";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                AllCarPeccancy allCarPeccancy = gson.fromJson(jsonObject.toString(), AllCarPeccancy.class);
                List<AllCarPeccancy.ROWSDETAILBean> rowsdetailBeen = allCarPeccancy.getROWS_DETAIL();
                mCarAllPeccancyNum = rowsdetailBeen.size();
                leng = rowsdetailBeen.size();
                for (int i = 0; i < leng - 1; i++) {
                    String d = rowsdetailBeen.get(i).getCarnumber();
                    for (int j = i + 1; j < leng; j++) {
                        if (d.equals(rowsdetailBeen.get(j).getCarnumber())) {
                            j--;
                            leng--;
                        }
                    }
                }
                mCarPeccancyNum = mCarAllPeccancyNum - leng;
                setData();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        requestQueue.add(request);
    }

    private void setData() {

        ArrayList<String> xVals = new ArrayList<String>();
        xVals.add("无重复违章");
        xVals.add("有重复违章");

        float no = leng / (mCarAllPeccancyNum + 0.0f);
        float yes = mCarPeccancyNum / (mCarAllPeccancyNum + 0.0f);
        ArrayList<Entry> yVals = new ArrayList<Entry>();
        yVals.add(new Entry(no * 100, 0));
        yVals.add(new Entry(yes * 100, 1));
        PieDataSet dataSet = new PieDataSet(yVals, "");
        dataSet.setSliceSpace(2f);
        dataSet.setSelectionShift(5f);

        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);

        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);

        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);
        dataSet.setSelectionShift(0f);

        PieData data = new PieData(xVals, dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
        mPieChart.setData(data);

        // undo all highlights
        mPieChart.highlightValues(null);

        mPieChart.invalidate();
    }
}
